package com.chenjl.generator.generated.impl;

import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.JavaVisibility;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.Parameter;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;

import com.chenjl.generator.Generator;
import com.chenjl.generator.generated.AbstractGenerated;
/**
 * 多个逻辑删除
 * 2020-4-8 17:04:57
 * @author chenjinlong
 */
public class BatchLogicDeleteByPrimaryKeyGenerated implements AbstractGenerated {
	
	
	@Override
	public Method clientGenerated(String baseRecordType) {
		//批量删除,batchLogicDeleteByPrimary
		Method batchLogicDeleteByPrimaryKeyMethod = new Method();
		batchLogicDeleteByPrimaryKeyMethod.addJavaDocLine("/**");
		batchLogicDeleteByPrimaryKeyMethod.addJavaDocLine(" * 批量逻辑删除,set is_deleted=1返回影响行数");
		batchLogicDeleteByPrimaryKeyMethod.addJavaDocLine(" */");
		batchLogicDeleteByPrimaryKeyMethod.setName("batchLogicDeleteByPrimaryKey");
		batchLogicDeleteByPrimaryKeyMethod.setVisibility(JavaVisibility.PUBLIC);
		batchLogicDeleteByPrimaryKeyMethod.setReturnType(new FullyQualifiedJavaType("java.lang.Integer"));
		batchLogicDeleteByPrimaryKeyMethod.addParameter(new Parameter(new FullyQualifiedJavaType("java.util.List<Long>"),"pkIds"));
		
		return batchLogicDeleteByPrimaryKeyMethod;
	}

	@Override
	public XmlElement sqlMapDocumentGenerated(IntrospectedTable introspectedTable,String tableName) {
		XmlElement batchLogicDeleteByPrimaryKeyForEachElement = new XmlElement("foreach");
		batchLogicDeleteByPrimaryKeyForEachElement.addAttribute(new Attribute("collection","list"));
		batchLogicDeleteByPrimaryKeyForEachElement.addAttribute(new Attribute("item","item"));
		batchLogicDeleteByPrimaryKeyForEachElement.addAttribute(new Attribute("index","index"));
		batchLogicDeleteByPrimaryKeyForEachElement.addAttribute(new Attribute("open","("));
		batchLogicDeleteByPrimaryKeyForEachElement.addAttribute(new Attribute("separator",","));
		batchLogicDeleteByPrimaryKeyForEachElement.addAttribute(new Attribute("close",")"));
		batchLogicDeleteByPrimaryKeyForEachElement.addElement(new TextElement(" ${item} "));
		
		XmlElement batchLogicDeleteByPrimaryKeyXmlElement = new XmlElement("update");
		batchLogicDeleteByPrimaryKeyXmlElement.addAttribute(new Attribute("id", "batchLogicDeleteByPrimaryKey"));
		batchLogicDeleteByPrimaryKeyXmlElement.addAttribute(new Attribute("parameterType","java.util.List"));
		batchLogicDeleteByPrimaryKeyXmlElement.addElement(new TextElement("update "+ tableName + " set "+Generator.LOGIC_DELETE_COLUMN_NAME+" = 1 where id in "));
		batchLogicDeleteByPrimaryKeyXmlElement.addElement(batchLogicDeleteByPrimaryKeyForEachElement);
		batchLogicDeleteByPrimaryKeyXmlElement.addElement(new TextElement("and "+Generator.LOGIC_DELETE_COLUMN_NAME+" = 0"));
		
		return batchLogicDeleteByPrimaryKeyXmlElement;
	}
}